package com.linc.multithread.utils.BlockDetect;

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.util.Log;


public class LogMonitor {
    private static LogMonitor sInstance = new LogMonitor();
    private HandlerThread mLogThread = new HandlerThread("log");
    private Handler mIoHandler;
    private static  final long TIME_BLOCK = 2000L;

    private LogMonitor(){
        mLogThread.start();
        mIoHandler = new Handler(mLogThread.getLooper());
    }

    private static Runnable mLogRunnable = new Runnable() {
        @Override
        public void run() {
            StringBuilder sb = new StringBuilder();
            StackTraceElement[] stackTrace = Looper.getMainLooper().getThread().getStackTrace();
            for(StackTraceElement s:stackTrace){
                sb.append(s.toString()+"\n");
            }
//            LogUtil.e("LogMonitor",sb.toString());
            Log.e("LogMonitor",sb.toString());
        }
    };

    public static LogMonitor getsInstance(){
        return sInstance;
    }

    //    public boolean isMonitor(){
//        return mIoHandler.hasCallbacks(mLogRunnable);
//    }

    public void startMonitor(){
        mIoHandler.postDelayed(mLogRunnable,TIME_BLOCK);
    }

    public void removeMonitor(){
        mIoHandler.removeCallbacks(mLogRunnable);
    }

}
